from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import View,TemplateView


def index(request):
    return HttpResponse("index")

class BookListView(View):
    def get(self,request,*args,**kwargs):
        return HttpResponse("book list view")

class AddBookView(View):
    def get(self,request,*args,**kwargs):
        return render(request,'front4/add_book.html')
    def post(self,request,*args,**kwargs):
        book_name = request.POST.get("name")
        book_author = request.POST.get("author")
        print({'name':book_name,'author':book_author})
        return HttpResponse("post")

class BookDetailView(View):
    def get(self,request,book_id):
        print(f"ID是：{book_id}")
        return HttpResponse("success")

    def dispatch(self, request, *args, **kwargs):
        print('dispatch')
        return super(BookDetailView,self).dispatch(request, *args, **kwargs)

    def http_method_not_allowed(self, request, *args, **kwargs):
        return HttpResponse("不支持除GET意外的其他请求")


class AboutView(TemplateView):
    template_name = 'about.html'

    def get_context_data(self, **kwargs):
        context = {"phone":"12345678"}
        return context